package com.whfc.emp.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.whfc.common.exception.BizException;
import com.whfc.common.result.PageData;
import com.whfc.common.result.ResultEnum;
import com.whfc.common.util.PageUtil;
import com.whfc.emp.dao.AppEmpBlackListMapper;
import com.whfc.emp.dao.AppEmpMapper;
import com.whfc.emp.dto.AppEmpBlackDTO;
import com.whfc.emp.dto.AppEmpDTO;
import com.whfc.emp.entity.AppEmp;
import com.whfc.emp.entity.AppEmpBlackList;
import com.whfc.emp.param.AppEmpBlackAddParam;
import com.whfc.emp.service.AppBlackService;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ClasssName AppBlackServiceImpl
 * @Description 黑名单管理
 * @Author hw
 * @Date 2020/12/2 14:13
 * @Version 1.0
 */
@DubboService(interfaceClass = AppBlackService.class, version = "1.0.0")
public class AppBlackServiceImpl implements AppBlackService {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private AppEmpBlackListMapper appEmpBlackListMapper;

    @Autowired
    private AppEmpMapper appEmpMapper;

    @Override
    public PageData<AppEmpBlackDTO> blackList(Integer deptId, Integer pageNum, Integer pageSize, String keyword) {
        logger.info("查询人员黑名单列表，deptId：{}，pageNum：{}，pageSize：{}，keyword:{}", deptId, pageNum, pageSize, keyword);
        PageHelper.startPage(pageNum, pageSize);
        List<AppEmpBlackDTO> empBlackDTOList = appEmpBlackListMapper.selectByDeptIds(deptId, keyword);
        for (AppEmpBlackDTO appEmpBlackDTO : empBlackDTOList) {
            Integer empId = appEmpBlackDTO.getEmpId();
            AppEmp appEmp = appEmpMapper.selectByPrimaryKey(empId);
            if (appEmp != null) {
                appEmpBlackDTO.setGroupName(appEmp.getGroupName());
            }
        }
        PageInfo<AppEmpBlackDTO> of = PageInfo.of(empBlackDTOList);
        return PageUtil.pageData(of);
    }

    @Override
    public void blackAdd(AppEmpBlackAddParam request) {
        logger.info("添加人员黑名单,request:{}", request);
        List<AppEmpDTO> empList = request.getEmpList();
        String reason = request.getReason();
        Date time = new Date();
        List<AppEmpBlackList> list = new ArrayList();
        for (AppEmpDTO emp : empList) {
            AppEmpBlackList empBlackList = new AppEmpBlackList();
            empBlackList.setDeptId(emp.getDeptId());
            empBlackList.setEmpId(emp.getEmpId());
            empBlackList.setEmpName(emp.getEmpName());
            empBlackList.setIdCardNo(emp.getIdCardNo());
            empBlackList.setReason(reason);
            empBlackList.setTime(time);
            empBlackList.setUserId(request.getUserId());
            empBlackList.setUserName(request.getUserName());
            list.add(empBlackList);
        }
        appEmpBlackListMapper.batchInsert(list);
    }

    @Override
    public void blackDel(Integer id) {
        logger.info("删除黑名单，id:{}", id);
        appEmpBlackListMapper.deleteLogicById(id);
    }

    @Override
    public List<AppEmpDTO> getEmpList(Integer deptId) {
        logger.info("查询黑名单:deptId:{}", deptId);
        return appEmpMapper.selectBlackEmpList(deptId);
    }

    @Override
    public void blackDelList(List<Integer> ids) {
        logger.info("批量删除黑名单，ids:{}", ids.toString());
        if (ids.size()<1) {
            throw new BizException(ResultEnum.PARAM_ERROR.getCode(),"参数验证失败");
        }
        appEmpBlackListMapper.deleteLogicByIds(ids);
    }
}
